package com.ruoyi.web.controller.mes;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.pinyin.PinYinUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.web.mapper.MesConfigMapper;
import com.ruoyi.web.mapper.OtherReportMapper;
import com.ruoyi.web.mapper.ProduceMapper;
import com.ruoyi.web.service.OtherReportDetailService;
import com.ruoyi.web.service.OtherReportService;
import com.ruoyi.web.service.OtherReportUserService;
import com.ruoyi.web.vo.*;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@RestController
@RequestMapping("/otherReport")
public class OtherReportController extends BaseController {

    @Resource
    private OtherReportService otherReportService;
    @Resource
    private OtherReportDetailService reportDetailService;
    @Resource
    private MesConfigMapper configMapper;
    @Resource
    private ProduceMapper produceMapper;
    @Resource
    private OtherReportDetailService otherReportDetailService;
    @Resource
    private OtherReportUserService otherReportUserService;
    @Resource
    private OtherReportMapper otherReportMapper;

    @GetMapping(value = "/list")
    public TableDataInfo list(OtherReport otherReport) {
        //startPage(params);
//        String code = otherReport.getCode();            // 报工单编码
//        String createBy = otherReport.getCreateBy();    // 上报人
//        Date date = null;
//        String status=otherReport.getStatus();
//        boolean datenull=false;
//        if (otherReport.getReportDate()!=null){
//            datenull=true;
//            date=otherReport.getReportDate();
//        }
//        TableDataInfo data = getDataTable(otherReportService.list(new QueryWrapper<OtherReport>().lambda()
//                .like(StringUtils.isNotEmpty(code), OtherReport::getCode, code)
//                .like(StringUtils.isNotEmpty(createBy), OtherReport::getCreateBy, createBy)
//                .eq(datenull,OtherReport::getReportDate, date)
//                .eq(StringUtils.isNotEmpty(status),OtherReport::getStatus,status)
//                .orderByDesc(OtherReport::getCreateTime)
//        ));
        startPage();
        List<OtherReport> otherReports = otherReportMapper.selectOtherReport(otherReport);
        return getDataTable(otherReports);
    }

    @PostMapping(value = "/getById")
    public AjaxResult getById(@RequestBody Map<String, String> params) {
        String id = params.get("id");
        OtherReport report = otherReportService.getById(id);
        List<OtherReportDetail> reportDetailList = reportDetailService.list(new QueryWrapper<OtherReportDetail>().lambda().eq(OtherReportDetail::getReportId, report.getId()).orderByDesc(OtherReportDetail::getCreateTime));
        List<OtherReportUser> reportUserList = otherReportUserService.list(new QueryWrapper<OtherReportUser>().lambda().eq(OtherReportUser::getReportId, report.getId()).orderByDesc(OtherReportUser::getCreateTime));

        report.setReportDetailList(reportDetailList);
        report.setReportUserList(reportUserList);
        return AjaxResult.success("查询成功", report);
    }

    @PostMapping(value = "/deleteById")
    public AjaxResult deleteById(@RequestBody Map<String, String> params) {
        String id = params.get("id");
        otherReportService.removeById(id);
        reportDetailService.remove(new QueryWrapper<OtherReportDetail>().lambda().eq(OtherReportDetail::getReportId, id));
        return AjaxResult.success("删除成功");
    }

    @PostMapping(value = "/add")
    @Transactional
    public AjaxResult add(@RequestBody OtherReport otherReport) {
        if (otherReport.getId() == null) {
            otherReport.setId(UUID.randomUUID().toString().replace("-", ""));
            otherReport.setCreateBy(SecurityUtils.getUsername());
            otherReport.setCreateTime(DateUtils.getNowDate());
            otherReport.setStatus("0");
//            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            Date date = formatter.parse(report.getReportDate());
            Map<String, Object> map = new HashMap<>();
            map.put("columnName", "code");
            map.put("tableName", "mes_report_other");
            int maxNum = configMapper.getMouthMaxId(map);
            String code = PinYinUtils.getCodeNormal(maxNum, "QTBG");
            otherReport.setCode(code);
        }else{
            otherReport.setUpdateTime(DateUtils.getNowDate());
            otherReport.setUpdateBy(SecurityUtils.getUsername());
        }
        if (otherReport.getReportDetailList() != null && otherReport.getReportDetailList().size() != 0) {
            int xh = 0;
            for (OtherReportDetail reportDetail : otherReport.getReportDetailList()) {
                xh = xh + 1;
                String xlh = PinYinUtils.sequence(xh);//序列号
                reportDetail.setReportId(otherReport.getId());
                reportDetail.setCode(otherReport.getCode() + "QT" + xlh);
                reportDetail.setCreateBy(SecurityUtils.getUsername());
                reportDetail.setCreateTime(DateUtils.getNowDate());
            }

        }
        otherReportService.saveOrUpdate(otherReport);

        if (otherReport.getReportUserList() != null && otherReport.getReportUserList().size() != 0) {
            for (OtherReportUser reportUser : otherReport.getReportUserList()) {
                reportUser.setReportId(otherReport.getId());
                reportUser.setCreateBy(SecurityUtils.getUsername());
                reportUser.setCreateTime(DateUtils.getNowDate());
            }

        }
        if (otherReport.getReportUserList() != null && otherReport.getReportUserList().size() != 0) {
            otherReportUserService.remove(new QueryWrapper<OtherReportUser>().lambda().eq(OtherReportUser::getReportId, otherReport.getId()));
            otherReportUserService.saveBatch(otherReport.getReportUserList());
        }

        if (otherReport.getReportDetailList() != null && otherReport.getReportDetailList().size() != 0) {
            otherReportDetailService.remove(new QueryWrapper<OtherReportDetail>().lambda().eq(OtherReportDetail::getReportId, otherReport.getId()));
            otherReportDetailService.saveBatch(otherReport.getReportDetailList());
        }
        return AjaxResult.success("添加成功", otherReport.getId());
    }

    @PostMapping(value = "/updateStatus")
    @Transactional
    public AjaxResult updateStatus(@RequestBody Map<String, String> params) {
        String id = params.get("id");
        String status = params.get("status");

        // 更新报工单状态
        otherReportService.update(new UpdateWrapper<OtherReport>().lambda().set(OtherReport::getStatus, status).eq(OtherReport::getId, id));
        return AjaxResult.success("");
    }


    @GetMapping(value = "/getProduceList")
    public TableDataInfo getProduceList(@RequestParam Map<String, String> params) {
        startPage(params);
        Produce produce=new Produce();
        produce.setProduceNo(params.get("produceNo"));
        produce.setProductName(params.get("produceName"));
        // 八开卷裁切，需要过滤
        String otherReport = params.get("otherReport");
        produce.setBookSize(Integer.valueOf(otherReport));
        List<Produce> list = produceMapper.getProduceList(produce);
        return  getDataTable(list);
    }
}
